import java.util.Iterator;
import java.util.Stack;

/**
 * 前序遍历迭代器
 */
public class TreeIterator implements Iterator<TreeNode> {
    private final Stack<TreeNode> stack;
    public TreeIterator(TreeNode root) {
        stack = new Stack<>();
        stack.push(root);
    }
    @Override
    public boolean hasNext() {
        return !stack.isEmpty();
    }

    @Override
    public TreeNode next() {
        TreeNode top = stack.pop();
        if(top.getRightChild()!= null)
            stack.push(top.getRightChild());
        if(top.getLeftChild()!= null)
            stack.push(top.getLeftChild());
        return top;
    }
}
